package com.dandelion.framework.handler;

import cn.hutool.json.JSONUtil;
import com.dandelion.framework.common.R;
import com.dandelion.framework.common.util.WebUtils;
import com.dandelion.framework.enums.CodeEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拒绝访问处理器impl
 *
 * @author 77088
 * @date 2022/02/16
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)  {
        R r = R.fail(CodeEnum.FORBIDDEN.getCode(), CodeEnum.FORBIDDEN.getMsg());
        //权限不足会调用此方法
        WebUtils.renderString(response, JSONUtil.toJsonStr(r));
    }
}